﻿

using com.iot.core.boot;
using com.iot.core.infrastructure;
using Microsoft.EntityFrameworkCore.Query;

namespace com.iot.core.data
{
    public partial class BaseService<TEntity> : IBaseService<TEntity> where TEntity : BaseEntity
    {
        IRepository<TEntity> _Repository;
        public BaseService() {
            _Repository= EngineContext.Current.Resolve<IRepository<TEntity>>();
        }
        public IRepository<TEntity>  Repository 
            => _Repository;
        public IRepository<TEntity> BaseMapper
           => _Repository;
         
        public IQueryable<TEntity> GetAll(bool includeDeleted = false)
        {
            return Repository.GetAll(includeDeleted);
        }
        public async Task<int> SaveChagnesAsync()
        {
            return await Repository.SaveChagnesAsync();
        }
        public int SaveChagnes()
        {
            return Repository.SaveChagnes();
        }
        public TEntity GetById(long id)
        {
            return Repository.GetById(id);
        }
        public async Task<TEntity> GetByIdAsync(long id)
        {
            return await  Repository.GetByIdAsync(id);    
        }

        public List<TEntity> GetByIds(List<long> lid)
        {
            return Repository.GetByIds(lid);
        }
        public async Task<List<TEntity>> GetByIdsAsync(List<long> lid)
        {
            return await  Repository.GetByIdsAsync(lid);
        }



        public int Insert(TEntity entity, bool autoField = true, bool autoSave = true)
        {
            Repository.Insert(entity, autoField);
            if (autoSave)
            {
                var iotHttpContext = EngineContext.Current.Resolve<MyContext>();
                if (iotHttpContext != null  )
                {
                    iotHttpContext.Inserted = entity;
                } 
                return Repository.SaveChagnes();
            }
            else
            {
                return 0;
            }
        }
        public async Task<int> InsertAsync(TEntity entity, bool autoField = true, bool autoSave = true)
        {
            Repository.Insert(entity, autoField);
            if (autoSave)
            {
                var iotHttpContext = EngineContext.Current.Resolve<MyContext>();
                if (iotHttpContext != null)
                {
                    iotHttpContext.Inserted = entity;
                }
                return await Repository.SaveChagnesAsync();
            }
            else
            {
                return 0;
            }
        }

        public int Insert(List<TEntity> lentity, bool autoField = true, bool autoSave = true)
        {
            if(lentity==null|| lentity.Count()==0)
            {
                return 0;
            }
            foreach(var entity in lentity)
            {
                Repository.Insert(entity, autoField);
            }
            
            if (autoSave)
            {
                var iotHttpContext = EngineContext.Current.Resolve<MyContext>();
                if (iotHttpContext != null)
                {
                    iotHttpContext.Inserteds = lentity as List<BaseEntity>;
                }
                return   Repository.SaveChagnes();
            }
            else
            {
                return 0;
            }
        }

        public async Task<int> InsertAsync(List<TEntity> lentity, bool autoField = true, bool autoSave = true)
        {
            if (lentity == null || lentity.Count() == 0)
            {
                return 0;
            }
            foreach (var entity in lentity)
            {
                Repository.Insert(entity, autoField);
            }

            if (autoSave)
            {
                var iotHttpContext = EngineContext.Current.Resolve<MyContext>();
                if (iotHttpContext != null)
                {
                    iotHttpContext.Inserteds = lentity as List<BaseEntity>;
                }
                return await Repository.SaveChagnesAsync();
            }
            else
            {
                return 0;
            }
        }


        public int Update(TEntity entity, bool autoField = true, bool autoSave = true)
        {
            Repository.Update(entity,autoField);
            if (autoSave)
            {
                var iotHttpContext = EngineContext.Current.Resolve<MyContext>();
                if (iotHttpContext != null)
                {
                    iotHttpContext.Inserted = entity;
                }
                return Repository.SaveChagnes();
            }
            else
            {
                return 0;
            }
        }
        public async Task<int> UpdateAsync(TEntity entity, bool autoField = true, bool autoSave = true)
        {
            Repository.Update(entity, autoField);
            if (autoSave)
            {
                var iotHttpContext = EngineContext.Current.Resolve<MyContext>();
                if (iotHttpContext != null)
                {
                    iotHttpContext.Inserted = entity;
                }
                return await Repository.SaveChagnesAsync();
            }
            else
            {
                return 0;
            }
        }

        public int Delete(TEntity entity, bool autoField = true, bool autoSave = true)
        {
            Repository.Delete(entity, autoField);
            if (autoSave)
            {
                var iotHttpContext = EngineContext.Current.Resolve<MyContext>();
                if (iotHttpContext != null)
                {
                    iotHttpContext.Deleted = entity;
                }
                return Repository.SaveChagnes();
            }
            else
            {
                return 0;
            }
        }
        public async Task<int> DeleteAsync(TEntity entity, bool autoField = true, bool autoSave = true)
        {
            Repository.Delete(entity,autoField);
            if (autoSave)
            {
                var iotHttpContext = EngineContext.Current.Resolve<MyContext>();
                if (iotHttpContext != null)
                {
                    iotHttpContext.Deleted = entity;
                }
                return await Repository.SaveChagnesAsync();
            }
            else
            {
                return 0;
            }
        }
        public int Delete(BaseQto qm)
        {
            return DeleteById(qm.Id, true, true);
        }


        public int DeleteById(long Id, bool autoField = true, bool autoSave = true)
        {
            var entity=Repository.GetById(Id);
            if (entity == null)
            {
                return 0;
            }
            return Delete(entity, autoField, autoSave);
        }
        public async Task<int> DeleteByIdAsync(long Id, bool autoField = true, bool autoSave = true)
        { 
            var entity = await Repository.GetByIdAsync(Id);
            if (entity == null)
            {
                return 0;
            }
            return await DeleteAsync(entity, autoField, autoSave);
        }

        public int DeleteByIds(List<long> lId, bool autoField = true, bool autoSave = true)
        {
            var lentity = Repository.GetByIds(lId);
            if (lentity == null || lentity.Count() == 0)
            {
                return 0;
            }
            foreach (var entity in lentity)
            {
                Repository.Delete(entity, autoField);
            }

            if (autoSave)
            {
                var iotHttpContext = EngineContext.Current.Resolve<MyContext>();
                if (iotHttpContext != null)
                {
                    iotHttpContext.Deleteds = lentity as List<BaseEntity>;
                }
                return   Repository.SaveChagnes();
            }
            else
            {
                return 0;
            }
        }
        public async Task<int> DeleteByIdsAsync(List<long> lId, bool autoField = true, bool autoSave = true)
        {
            var lentity =await  Repository.GetByIdsAsync(lId);
            if (lentity == null || lentity.Count() == 0)
            {
                return 0;
            }
            foreach (var entity in lentity)
            {
                Repository.Delete(entity, autoField);
            }

            if (autoSave)
            {
                var iotHttpContext = EngineContext.Current.Resolve<MyContext>();
                if (iotHttpContext != null)
                {
                    iotHttpContext.Deleteds = lentity as List<BaseEntity>;
                }
                return Repository.SaveChagnes();
            }
            else
            {
                return 0;
            }
        }

        public List<T> QueryToList<T>(string sql, int CommandTimeout = 30) where T : new()
        {
            return Repository.QueryToList<T>(sql, CommandTimeout);
        }
        public List<IdNameDto> QueryName(List<long> lid, string tbname = "", string namefiled = "")
        {
            return Repository.QueryName(lid,tbname,namefiled);  
        }
        public string GetName(long id, string tbname = "", string namefiled = "")
        {
            return Repository.GetName(id, tbname, namefiled);
        }


        public void EntityToDto(object src, object dst)
        {
            Repository.EntityToDto(src, dst);       
        }
        public void DtoToEntity(object src, object dst)
        {
            Repository.DtoToEntity(src, dst);
        }
         
    }
}
